package pers.cnzdy.tutorial.Chapter9_Perception_Media.Section1_Sensor;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.EditText;

import java.util.ArrayList;
import java.util.List;

import pers.cnzdy.tutorial.R;

// implements SensorEventListener
public class SensorActivity extends AppCompatActivity {
    public static String TAG = "SensorActivity";
    private SensorManager sensorManager;

    EditText editTextGyro;
    EditText editTextMagnetic;
    EditText editTextGravity;
    EditText editTextAcceleration;
    EditText editTextTemerature;
    EditText editTextLight;
    EditText editTextPressure;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sensor);

        editTextGyro = findViewById(R.id.edit_gyro);
        editTextMagnetic = findViewById(R.id.edit_text_magnetic);
        editTextGravity = findViewById(R.id.edit_text_gravity);
        editTextAcceleration = findViewById(R.id.edit_text_acceleration);
        editTextTemerature = findViewById(R.id.edit_text_temerature);
        editTextLight = findViewById(R.id.edit_text_light);
        editTextPressure = findViewById(R.id.edit_text_pressure);

        List<Sensor> sensorList;
        sensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);

        // 得到设置支持的所有传感器的List
        sensorList = sensorManager.getSensorList(Sensor.TYPE_ALL);
        List<String> sensorNameList = new ArrayList<String>();

        for (Sensor sensor : sensorList) {
            Log.d(TAG, "onResume: " + sensor.getName());
        }
    }

    private SensorEventListener listener = new SensorEventListener() {
        @Override
        public void onSensorChanged(SensorEvent sensorEvent) {
            float[] values = sensorEvent.values;
            // 获取触发event的传感器类型
            int sensorType = sensorEvent.sensor.getType();
            StringBuilder sb = null;
            // 判断是哪个传感器发生改变
            switch (sensorType)
            {
                // 陀螺仪传感器
                case Sensor.TYPE_GYROSCOPE:
                    sb = new StringBuilder();
                    sb.append("绕X轴旋转的角速度：");
                    sb.append(values[0]);
                    sb.append("\n绕Y轴旋转的角速度：");
                    sb.append(values[1]);
                    sb.append("\n绕Z轴旋转的角速度：");
                    sb.append(values[2]);
                    editTextGyro.setText(sb.toString());
                    break;
                // 磁场传感器
                case Sensor.TYPE_MAGNETIC_FIELD:
                    sb = new StringBuilder();
                    sb.append("X轴方向上的磁场强度：");
                    sb.append(values[0]);
                    sb.append("\nY轴方向上的磁场强度：");
                    sb.append(values[1]);
                    sb.append("\nZ轴方向上的磁场强度：");
                    sb.append(values[2]);
                    editTextMagnetic.setText(sb.toString());
                    break;
                // 重力传感器
                case Sensor.TYPE_GRAVITY:
                    sb = new StringBuilder();
                    sb.append("X轴方向上的重力：");
                    sb.append(values[0]);
                    sb.append("\nY轴方向上的重力：");
                    sb.append(values[1]);
                    sb.append("\nZ方向上的重力：");
                    sb.append(values[2]);
                    editTextGravity.setText(sb.toString());
                    break;
                // 线性加速度传感器
                case Sensor.TYPE_LINEAR_ACCELERATION:
                    sb = new StringBuilder();
                    sb.append("X轴方向上的线性加速度：");
                    sb.append(values[0]);
                    sb.append("\nY轴方向上的线性加速度：");
                    sb.append(values[1]);
                    sb.append("\nZ轴方向上的线性加速度：");
                    sb.append(values[2]);
                    editTextAcceleration.setText(sb.toString());
                    break;
                // 温度传感器
                case Sensor.TYPE_AMBIENT_TEMPERATURE:
                    sb = new StringBuilder();
                    sb.append("当前温度为：");
                    sb.append(values[0]);
                    editTextTemerature.setText(sb.toString());
                    break;
                // 光传感器
                case Sensor.TYPE_LIGHT:
                    sb = new StringBuilder();
                    sb.append("当前光的强度为：");
                    sb.append(values[0]);
                    editTextLight.setText(sb.toString());
                    break;
                // 压力传感器
                case Sensor.TYPE_PRESSURE:
                    sb = new StringBuilder();
                    sb.append("当前压力为：");
                    sb.append(values[0]);
                    editTextPressure.setText(sb.toString());
                    break;
            }
        }

        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {
        }
    };

    @Override
    protected void onResume() {
        super.onResume();
        
        // 为系统的陀螺仪传感器注册监听器
        sensorManager.registerListener(listener,
                sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE),
                SensorManager.SENSOR_DELAY_GAME);

        // 为系统的磁场传感器注册监听器
        sensorManager.registerListener(listener,
                sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),
                SensorManager.SENSOR_DELAY_GAME);

        // 为系统的重力传感器注册监听器
        sensorManager.registerListener(listener,
                sensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY),
                SensorManager.SENSOR_DELAY_GAME);

        // 为系统的线性加速度传感器注册监听器
        sensorManager.registerListener(listener,
                sensorManager.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION),
                SensorManager.SENSOR_DELAY_GAME);

        // 为系统的温度传感器注册监听器
        sensorManager.registerListener(listener,
                sensorManager.getDefaultSensor(Sensor.TYPE_AMBIENT_TEMPERATURE),
                SensorManager.SENSOR_DELAY_GAME);

        // 为系统的光传感器注册监听器
        sensorManager.registerListener(listener,
                sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT),
                SensorManager.SENSOR_DELAY_GAME);

        // 为系统的压力传感器注册监听器
        sensorManager.registerListener(listener,
                sensorManager.getDefaultSensor(Sensor.TYPE_PRESSURE),
                SensorManager.SENSOR_DELAY_GAME);
    }

    @Override
    protected void onStop() {
        sensorManager.unregisterListener(listener);
        super.onStop();
    }

    @Override
    protected void onPause()
    {
        // 程序暂停时取消注册传感器监听器
        sensorManager.unregisterListener(listener);
        super.onPause();
    }
}